package com.yueya.basic.notice.action;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.notice.model.Notice;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;

/**
 * 
 * 
 * @author Administrator
 * 
 */
public class NoticeRecycleController extends BaseController {

	@SuppressWarnings("rawtypes")
	Map map = new HashMap();

	public void index() {

	}

	public void list() {// 通知公告管理
		this.setAttr("flag", this.getPara("flag"));
		render("/WEB-INF/views/basic/notice/recycle_list.jsp");
	}

	@SuppressWarnings("unchecked")
	public void getListPager() {
		Record user = getUserWithCookie();
		String userid = user.getStr("user_id");

		map.clear();
		map.put("name", this.getPara("name"));
		map.put("flag", this.getPara("flag"));
		map.put("delflag", "1");
		map.put("userid", userid);
		Page<Record> page = Notice.service
				.pagination(getPage(), getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void view() {
		String id = this.getPara("id");
		Record notice = Notice.service.getNoticeById(id);
		setAttr("tzgg", notice);
		render("/WEB-INF/views/basic/notice/view.jsp");
	}

	public void doDelete() {

		Notice tzgg = Notice.dao.findById(getPara("id"));
		boolean bl = false;
		if (tzgg != null) {
			bl = tzgg.delete();
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("彻底删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("彻底删除失败!");
		}
		this.renderJson(j);
	}

	public void doRecovery() {

		int num = Db.update("update t_notice set delflag='0' where id =?",
				getPara("id"));
		Json j = new Json();
		if (num > 0) {
			j.setSuccess(true);
			j.setMsg("恢复成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("恢复失败!");
		}
		this.renderJson(j);
	}

	@Before(Tx.class)
	public void bathdelete() {
		Boolean bl = false;

		String idsStr = this.getPara("ids");

		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Notice tzgg = null;
			for (int i = 0; i < ids.length; i++) {

				if (!"".equals(ids[i]) && ids[i] != null) {
					tzgg = Notice.dao.findById(ids[i]);
					bl = tzgg.delete();
					if (!bl) {
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("彻底删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("彻底删除失败!");
		}
		this.renderJson(j);
	}

}
